package proyecto.gestionarDatosDeTemas;
    
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import proyecto.BusinessLogic.TemaBLRemote;
import proyecto.EntityBeans.Tema;

import util.Common;
      
/**
 * @author Administrador
 *
 */
public class AltaTemaAction extends Action {
		
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,			
			HttpServletRequest request,
			HttpServletResponse response) {
						
			TemaBLRemote temaBLRemote = null;
			final Context context;
		
			TemaForm temaForm = (TemaForm) form;			
											
			if (isCancelled(request)){				
				return mapping.findForward(Common.SHOWLIST);
			}
			else{			
				try
				{							
					context = new InitialContext();					
					temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
				} catch (NamingException e)				{
					e.printStackTrace();
					throw new RuntimeException(e);				
				}	
				//Alta de Nivele				
				Tema tema = new Tema(temaForm.getNombre(),temaForm.getDescripcion(),new Date());				
				int temaNuevo = temaBLRemote.save(tema); //alta de tema
				System.out.println("nuevo tema insertado " + temaNuevo); 
				
				return mapping.findForward(Common.SUCCESS);				
		}
	}
}

